package com.sat.order.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.sat.goods.feign.GoodsFeign;
import com.sat.order.dao.OrderItemMapper;
import com.sat.order.pojo.Order;
import com.sat.order.pojo.OrderItem;
import com.sat.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Configuration
@RabbitListener(queues = {"add_order_queue_dead","${mq.pay.queue.order}"})
public class OrderPayMessageListener {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsFeign goodsFeign;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @RabbitHandler
    public void consumeMessage(String msg) throws Exception{

        System.out.println(msg);
        //将数据转成Map
        Map<String,String> result = JSON.parseObject(msg,Map.class);

        //return_code=SUCCESS
        String return_code = result.get("return_code");
        //业务结果
        String result_code = result.get("result_code");

        //业务结果 result_code=SUCCESS/FAIL，修改订单状态
        if(return_code.equalsIgnoreCase("success") ){
            //获取订单号
            String outtradeno = result.get("out_trade_no");
            //业务结果
            if(result_code.equalsIgnoreCase("success")){
                if(outtradeno!=null){
                    //修改订单状态  out_trade_no
                    orderService.updateStatus(outtradeno,result.get("time_end"),result.get("transaction_id"));
                }
            }else{
                //支付失败,删除订单，回滚库存
                orderService.deleteOrder(outtradeno);
                //TODO 关闭支付  p270结尾，调用微信关闭订单接口
            }
         }
    }









    /**
     * 监听死信队列
     * @param message
     * @param channel
     * @throws Exception
     */
    @RabbitHandler
    public void getMsg(Message message, Channel channel) throws Exception {
        String orderId = new String(message.getBody(),"UTF-8");
        Order order = orderService.findById(orderId);

        //未支付或者支付失败修改订单，可以封装成方法
        if (!"1".equals(order.getPayStatus())) {

            order.setOrderStatus("0");  //设置订单为未完成
            order.setPayStatus("0");
            // TODO 回滚库存...
            //根据orderid查找到对应的orderItem
            Example example = new Example(OrderItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId",orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            for (OrderItem orderItem : orderItems) {
                Long skuId = orderItem.getSkuId();
                Integer num = orderItem.getNum();
                goodsFeign.rollBackSkuStock(skuId,num);

            }
            // TODO 调用短信微服务器

            //修改订单状态
            orderService.update(order);

        }
    }
}
